package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.dto.ServiceRecordPdfOrExcelCollDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@IocBean
@At("/platform/sqfw/formSummary/new")
@ApiDefinition(tag = "消防信息")
@Slf4j
public class SqfwFormsummaryNewController{
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private SqfwCompleteflowService sqfwCompleteflowService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询消防信息")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Sqfw_formSummary_new.class
    )
    @ApiResponses(
    implementation = Sqfw_formSummary_new.class
    )
//    @SaCheckPermission("sqfw.formSummary.new")
    public Result<?> list(@Param("..")Sqfw_formSummary_new bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        if (Strings.isNotBlank(bean.getSqfwId())) {
            cnd.and("sqfw_id", "like", "%" + bean.getSqfwId() + "%");
        }
        if (Strings.isNotBlank(bean.getCommunity())) {
            cnd.and("community", "like", "%" + bean.getCommunity() + "%");
        }
        //json 查询

         cnd.and( new Static("  paramdata->'common'->>'unit_name' = '测试单位'") );

        if (bean.getUnitName() !=null && Strings.isNotBlank(bean.getUnitName().toString())) {
            cnd.and( new Static(" and paramdata->'common'->>'unit_name' like'%"+bean.getUnitName()+"%'") );
        }
        if (bean.getUnitName() !=null && Strings.isNotBlank(bean.getUnitName().toString())) {
            cnd.and( new Static(" and paramdata->'common'->>'unit_name' in（ '"+bean.getUnitName()+"')") );
        }
        if (bean.getUnitName() !=null && Strings.isNotBlank(bean.getUnitName().toString())) {
            cnd.and( new Static("paramdata @> '{\"unit_name\":\"张明\"}'") );
        }

        return Result.data(sqfwFormsummaryNewService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增消防信息")
    @ApiFormParams(
        implementation = Sqfw_formSummary_new.class
    )
    @ApiResponses
    @SLog(tag = "新增消防信息", value = "${args[0].id}")
    @SaCheckPermission("sqfw.formSummary.new.create")
    public Result<?> create(@Param("..")Sqfw_formSummary_new sqfwFormsummaryNew, HttpServletRequest req) {
        sqfwFormsummaryNew.setCreateBy(SecurityUtil.getUserId());
        sqfwFormsummaryNewService.insert(sqfwFormsummaryNew);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改消防信息")
    @ApiFormParams(
        implementation = Sqfw_formSummary_new.class
    )
    @ApiResponses
    @SLog(tag = "修改消防信息", value = "${args[0].id}")
    @SaCheckLogin
    //@SaCheckPermission("sqfw.formSummary.new.update")
    public Result<?> update(@Param("..")Sqfw_formSummary_new sqfwFormsummaryNew, HttpServletRequest req) {
        sqfwFormsummaryNew.setUpdateBy(SecurityUtil.getUserId());
        sqfwFormsummaryNewService.updateIgnoreNull(sqfwFormsummaryNew);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取消防信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckLogin
    //@SaCheckPermission("sqfw.formSummary.new")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Sqfw_formSummary_new sqfwFormsummaryNew = sqfwFormsummaryNewService.fetch(id);
        if (sqfwFormsummaryNew == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sqfwFormsummaryNew);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除消防信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除消防信息", value = "${args[0]}")
    @SaCheckPermission("sqfw.formSummary.new.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Sqfw_formSummary_new sqfwFormsummaryNew = sqfwFormsummaryNewService.fetch(id);
        if (sqfwFormsummaryNew == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwFormsummaryNewService.delete(id);
        req.setAttribute("_slog_msg", sqfwFormsummaryNew.getId());
        return Result.success();
    }

    /**
     * 删除巡查记录
     *
     * @param id
     * @return
     */
    @At("/deleteById")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除表数据")
    @SLog(tag = "删除表数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> deleteById(@Param("id") String id) {
        Sqfw_formSummary_new sqfwFormSummary = sqfwFormsummaryNewService.fetch(id);
        if (sqfwFormSummary == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwCompleteflowService.deleteAndUpdateWO(sqfwFormSummary.getSqfwFlowId());
        return Result.success();
    }

    @At()
    @Ok("json")
    @DELETE
    @ApiOperation(name = "批量删除删除formSummaryNew表数据")
    @SLog(tag = "批量删除删除formSummaryNew表数据", value = "${args[0]}")
    @ApiFormParams({@ApiFormParam(name = "formSummaryIds", description = "ID数组")})
    @SaCheckLogin
    public Result<?> batchDeleteFormSummary(@Param("formSummaryIds") List<String> formSummaryIds) {
        log.info("batchDeleteFormSummary param: {}", formSummaryIds);
        sqfwFormsummaryNewService.batchDeleteFormSummary(formSummaryIds);
        return Result.success();
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "pdf合并")
    @ApiResponses(implementation = DownFileTypesResVo.class)
    @SaCheckLogin
    public Result<String> mergePDF(@Param("conditionList") List<ServiceRecordPdfOrExcelCollDTO> conditionList) {
        log.info("Controller mergePDF");
        return sqfwFormsummaryNewService.mergePDF(conditionList);
    }
}
